import java.io.*;
import java.util.*;

public class Test1 {
    public  static  final int N = 1010;
    public  static int [] S = new int[N];
    public  static int [][]Graph = new int[N][N];
    public  static int []indegree = new int[N];
    public  static int []st = new int [N];
    public  static int top,n,m,s,res = 0;
    public  static boolean [] vis1 = new boolean[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int s;
        for(int i = 1;i <= m;i ++){
             boolean [] vis = new boolean[N];
             s = sc.nextInt();
             for(int j = 1;j <= s;j ++){
                 S[j] = sc.nextInt();
                 vis[S[j]] = true;
             }
             for(int j = S[1];j <= S[s];j ++ ){
                 if(vis[j] == false){
                     for(int k = 1;k <= s;k ++){
                         if(Graph[S[k]][j] == 0){
                             Graph[S[k]][j] = 1;
                             indegree[j] ++;
                         }
                     }
                 }
             }
        }
        do
        {
            top = 0;
            for(int i = 1;i <= n;i ++){
                if(indegree[i] == 0 &&!vis1[i]){
                    st[++ top] = i;
                    vis1[i] = true;
                }///将入度为0的全部入栈
            }
            for(int i = 1;i <= top;i ++){
                for(int j = 1;j <= n;j ++){
                    if(Graph[st[i]][j] == 1){
                        Graph[st[i]][j] = 0;
                        indegree[j] --;
                    }///将所有指向st[i]的顶点入度-1
                }
            }
            res ++;

        }while(top >= 1);
        System.out.println(res - 1);
    }

}
